package com.sgb.kjwl.view.ui.adapter.manage;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.sgb.kjwl.databinding.ItemJingYingMapFindBinding;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfEntity;
import java.util.ArrayList;
import java.util.List;

/**
 * 企业经营 地图查找 底部recyclerview
 */
public class JingYingMapDataAdapter extends RecyclerView.Adapter<JingYingMapDataAdapter.ViewHolder> {
    private Context context;
    private List<TheEnterpriseOfEntity> datas;
    private final LayoutInflater inflater;
    private Callback callback;
    private String firstTabType="全部";
    private String secondTabType="全部";
    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public JingYingMapDataAdapter(Context context, List<TheEnterpriseOfEntity> list) {
        this.context = context;
        datas = list;
        inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemJingYingMapFindBinding binding = ItemJingYingMapFindBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TheEnterpriseOfEntity data = datas.get(position);
        holder.getBinding().setBean(data);

        String people =data.getStaffNumber();
        if(!TextUtils.isEmpty(people)){
            if (Integer.parseInt(people)!=0){
                holder.getBinding().tvPeopleS.setText(people+"人");
            }
        }

        String s_type = data.getServiceType();
        List<String> list = sortArray(s_type, firstTabType);
        holder.getBinding().setList(list);


        String s_model = data.getBusinessModel();
        List<String> list1 = sortArray(s_model, secondTabType);
        holder.getBinding().setList1(list1);

        holder.getBinding().tvDis.setText(data.getDistance()+"km");
        holder.getBinding().ivClose.setOnClickListener(v -> callback.backHidden());
        holder.getBinding().tvAttation.setOnClickListener(v -> callback.backAttention(data));
        holder.itemView.setOnClickListener(v -> callback.back(data));
        String one = data.getIndustryCategoryOneName();
        String two = data.getIndustryCategoryTwoName();
        StringBuilder sb= new StringBuilder();
        if(!TextUtils.isEmpty(one)){
            sb.append(one);
        }
        if(!TextUtils.isEmpty(two)){
            sb.append("/");
            sb.append(two);
        }
        String h = sb.toString();
        if(!TextUtils.isEmpty(h)){
            holder.getBinding().tvHangYeType.setText(h);
        }
        holder.getBinding().tvApply.setOnClickListener(v -> {
            callback.backApply(data);
        });
        holder.getBinding().tvJiaMeng.setOnClickListener(v -> {
            callback.backJiaMeng(data);
        });
        holder.getBinding().tvHanXun.setOnClickListener(v -> {
            callback.backHanXun(data);
        });
        holder.getBinding().executePendingBindings();
    }

    //字符串拆分排序
    public List<String> sortArray(String arg, String keywork) {
        List<String> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        list.clear();
        list1.clear();
        if(!TextUtils.isEmpty(arg)){
            String[] s_p = arg.split(",");
            String str;
            String str1 = "";
            int index = -1;
            if (s_p.length > 0) {
                for (int i = 0; i < s_p.length; i++) {
                    str = s_p[i].trim();
                    if (!TextUtils.isEmpty(str)) {
                        if (!keywork.equals("全部")) {
                            if (str.equals(keywork)) {
                                str1 = str;
                                index = i;
                            }
                        }
                        list.add(str);
                    }
                }
                if (!TextUtils.isEmpty(str1) && index != -1) {
                    list.remove(index);
                    list1.add(str1);
                }
                list1.addAll(list);
            }
        }
        return list1;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ItemJingYingMapFindBinding mBinding;

        public ItemJingYingMapFindBinding getBinding() {
            return mBinding;
        }

        public ViewHolder(ItemJingYingMapFindBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }
    }
    public void setFirstTabType(String name){
        this.firstTabType = name;
    }

    public void setSecondTabType(String name){
        this.secondTabType = name;
    }

    public interface Callback {
        void back(TheEnterpriseOfEntity bean);

        void backHidden();//关闭

        void backAttention(TheEnterpriseOfEntity bean);//关注

        void backJiaMeng(TheEnterpriseOfEntity bean);//加盟

        void backApply(TheEnterpriseOfEntity bean);//申请

        void backHanXun(TheEnterpriseOfEntity bean);//函询
    }

}